package com.lu.system.management.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.lu.common.utils.*;
import com.lu.system.management.other.RegisterMail;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lu.system.management.domain.UserDO;
import com.lu.system.management.service.UserService;

/**
 * 
 * 
 * @author showlu
 * @email 1309617271@163.com
 * @date 2018-08-14 13:42:34
 */
 
@Controller
@RequestMapping("/system/management/user")
public class UserController {
	@Autowired
	private UserService userService;

   /**
    *  根据id查询
	*  @return
	*/
    @ResponseBody
    @GetMapping("/get")
    public R get(Long userId){
        UserDO user = userService.get(userId);
        R r = new R();
        r.put("data",user);
        return r;
    }

	/**
	 * 查询列表数据
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	public PageUtils getUserList(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
		List<UserDO> userList = userService.list(query);
		int total = userService.count(query);
		PageUtils pageUtils = new PageUtils(userList, total);
		return pageUtils;
	}

	/**
	 * 注册前的手机短信验证
	 */
	@ResponseBody
	@GetMapping("/preSave")
	public R preSave(){
		String code = getRandNum().toString();
		JuheUtils.getRequest2("GET",code);
		R r = new R();
		r.put("data",code);
		return r;
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(UserDO user){
		user.setPassword(MD5Utils.encrypt(user.getUsername(),user.getPassword()));
		if(userService.save(user)>0){
			RegisterMail registerMail = new RegisterMail(user.getUsername(),user.getEmail());
			new Thread(registerMail).start();
			return R.ok();
		}
		return R.error();
	}

    /**
     * 批量保存
     */
    @ResponseBody
    @PostMapping("/saveBatch")
    public R saveBatch( List<UserDO> user){
        try {
			userService.saveBatch(user);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( UserDO user){
		userService.update(user);
		return R.ok();
	}

    /**
     * 批量修改
     */
    @ResponseBody
    @PostMapping("/updateBatch")
    public R updateBatch( List<UserDO> user){
        try {
			userService.updateBatch(user);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok();
    }

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove( Long userId){
		if(userService.remove(userId)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] userIds){
		userService.batchRemove(userIds);
		return R.ok();
	}

	public static Integer getRandNum() {
		int min = 1;
		int max = 999999;
		int randNum = min + (int)(Math.random() * ((max - min) + 1));
		return randNum;
	}

	public static void main(String[] args) {
		System.out.println(getRandNum());
	}

}
